/*
 * SpecialCommandInterpreter.cpp
 *
 *  Created on: 2 Apr 2014
 *      Author: christian.holzreuter
 */

#include "SpecialCommandInterpreter.h"
#include <iostream>

namespace solver {

using namespace std;

vector<const Command*> SpecialCommandInterpreter::commands;
bool SpecialCommandInterpreter::initialized = false;


SpecialCommandInterpreter::SpecialCommandInterpreter(void)
{
}


SpecialCommandInterpreter::~SpecialCommandInterpreter(void)
{
}


void SpecialCommandInterpreter::initialize(void)
{
	addCommand(new ExitCommand());
	addCommand(new InfoCommand());
	initialized = true;
}


void SpecialCommandInterpreter::interpret(const string& input)
{
	if (!initialized)
		initialize();


	for (unsigned int i = 0; i < commands.size(); i++) {
		if (commands[i]->matches(input)) {
			commands[i]->execute("");
			return;
		}
	}
	cout << "unknown command: " << input << "\n";
}


void SpecialCommandInterpreter::addCommand(const Command* command)
{
	commands.push_back(command);
}


Command::Command(const string& command)
{
	this->command = command;
}


Command::~Command(void)
{
}


bool Command::matches(const std::string& command) const
{
	return (command.size() >= this->command.size() + 1) &&
			command == ("\\" + this->command);
}


ExitCommand::ExitCommand(void) :
		Command("exit")
{
}


void ExitCommand::execute(const string& arguments) const
{
	::exit(0);
}


InfoCommand::InfoCommand(void) :
		Command("info")
{
}


void InfoCommand::execute(const string& arguments) const
{
	std::cout << "Mathy version 1.0\n" <<
			"";
}


} /* namespace Solver */
